package com.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.product.enmu.DeletedEnum;
import com.product.entity.OperationLog;
import com.product.mapper.OperationLogMapper;
import com.product.service.IOperationLogService;
import com.product.utils.PageUtils;
import com.product.utils.ThreadLocalUtils;
import com.product.vo.OperatorLogVo;
import com.product.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {
    private List<Integer> operatorIds;

    @Override
    public PageUtils getAllOperator(Integer size, Integer current, Integer userId, Integer operatorType, Integer timeOrderBy) {
        Page<OperationLog> operationPage = new Page<>(current, size);
        LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq((userId != null), OperationLog::getUserid, userId);
        queryWrapper.eq(operatorType != null, OperationLog::getOperatorType, operatorType);
        queryWrapper.orderBy(timeOrderBy != null, timeOrderBy != null && timeOrderBy == 0, OperationLog::getCreationTime);

        Page<OperationLog> page = this.baseMapper.selectPage(operationPage, queryWrapper);

        List<OperatorLogVo> operatorLogList = page.getRecords().stream().map(item -> {
            OperatorLogVo productVo = new OperatorLogVo();
            BeanUtils.copyProperties(item, productVo);
            return productVo;
        }).toList();
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(operatorLogList);
        return pageUtils;
    }

    @Override
    public void deleteOperator(Integer operatorId) {
        LambdaUpdateWrapper<OperationLog> updateWrapper = new LambdaUpdateWrapper<OperationLog>()
                .eq(OperationLog::getOperationLogId, operatorId)
                .set(OperationLog::getDeleted, DeletedEnum.DELETE.getValue());
        this.baseMapper.update(updateWrapper);
    }

    @Override
    public void deleteOperators(List<Integer> operatorIds) {
        this.operatorIds = operatorIds;
        operatorIds.forEach(this::deleteOperator);
    }

    @Override
    public List<OperationLog> getLogByUserId(Integer userId) {
        LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OperationLog::getUserid, userId);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public void addOperation(Integer operatorType, String message) {
        OperationLog operationLog = new OperationLog();
        operationLog.setOperatorType(operatorType);
        operationLog.setDescription(message);

        UserVo userVo = ThreadLocalUtils.get();
        operationLog.setUserid(userVo.getUserId());
        this.baseMapper.insert(operationLog);
    }
}
